import os
import sys
from tkinter import Label
import art
import cv2
import 游戏页面
import pygame
# from XiaoXiaoLe import *
# from XiaoXiaoLe.游戏主程序 import pacerGame
from 游戏主程序 import pacerGame
from tkinter import Tk
from PIL import ImageTk, Image
import pyttsx3 as pyttsx


def main(game=None):
    pygame.init()
    screen = pygame.display.set_mode(游戏页面.SCREENSIZE)
    pygame.display.set_caption('消消乐')
  
    # 加载音效，消除音效
    sounds = {}
    sounds['mismatch'] = pygame.mixer.Sound(os.path.join(游戏页面.ROOTDIR, 'res/audios/badswap.wav'))
    sounds['match'] = []
    for i in range(6):
        sounds['match'].append(pygame.mixer.Sound(os.path.join(游戏页面.ROOTDIR, 'res/audios/match%s.wav' % i)))

    # 字体显示
    font = pygame.font.Font(os.path.join(游戏页面.ROOTDIR, 'res/font/font.TTF'), 25)
    # 星星
    pacer_imgs = []
    for i in range(1, 8):
        pacer_imgs.append(os.path.join(游戏页面.ROOTDIR, 'res/imgs/%s.png' % i))
    # 循环
    game = pacerGame(screen, sounds, font, pacer_imgs, 游戏页面)
    while True:
        score = game.start()
        flag = False
        # 游戏时间结束后给出选择，玩家选择重玩或者退出
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE):
                    engine = pyttsx.init()
                    engine.say('Game over')
                    engine.runAndWait()
                    pygame.quit()
                    sys.exit()
                elif event.type == pygame.KEYUP and event.key == pygame.K_r:
                    flag = True
            if flag:
                engine = pyttsx.init()
                engine.say('Restart the game')
                engine.runAndWait()
                break
            screen.fill((136, 207, 236))
            text0 = 'Final score: %s' % score
            text1 = 'Press <R> to restart the game.'
            text2 = 'Press <Esc> to quit the game.'
            y = 150
            for idx, text in enumerate([text0, text1, text2]):
                text_render = font.render(text, 1, (85, 65, 0))
                rect = text_render.get_rect()
                if idx == 0:
                    # rect.left, rect.top = (223, y)
                    rect.left, rect.top = (200, y)
                elif idx == 1:
                    rect.left, rect.top = (96.5, y)
                else:
                    rect.left, rect.top = (93.5, y)
                y += 99
                screen.blit(text_render, rect)
            pygame.display.update()
        game.reset()


'''游戏运行'''
if __name__ == '__main__':

    main()